<?php
namespace app\admin\controller;



use think\Db;
use think\Controller;

class Goods  extends  Base
{
    public function index()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('goods');

            $res = $gd->page($page, $limit)
                ->order('id', 'desc')
                ->select();
//            foreach ($res as $index => $re) {
//                $res[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
//            }

//            $qiniu_config = cmf_get_option('qiniu');
//          // 七牛样式处理
//            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/80x120/quality/100/interlace/0";
//
//           foreach ($res as $index => $re) {
//                $res[$index]['pic'] = $qiniu_config['domain'] . '/' . $re['pic'] . $q_thumb;
//            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('goods')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function edit()
    {

        if (request()->isAjax()) {
            $data = input('');
            $res = Db::name('goods')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('goods')->find($id);
            $this->assign('item', $res);
            return $this->fetch();
        }


    }


    public function add()
    {

        if (request()->isAjax()) {
            $data = input('');
            $res = Db::name('goods')->insertGetId($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        } else {
            return $this->fetch();
        }


    }



    public function base_so()
    {
        $status[0] = '禁用';
        $status[1] = '正常';
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $key = trim(input('keyword'));
        if (!$key) {
            $this->error('没有内容');
        }

        $res = Db::name('goods')->page($page, $limit)
            //搜索相关
            //参数绑定
            // ->where('id > :id AND name LIKE :name ', ['id' => 0, 'name' => 'thinkphp%'])
            ->where(' name LIKE :key ', [ 'key' => "%$key%"])
            ->select();

        //根据关键字返回 相应数据
        $total =
            Db::name('goods')
                ->where(' name LIKE :key ', ['key' => "%$key%"])
                ->count();
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);
    }

}
